﻿using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;

using DreamCube.WinForm.Common;
using DreamCube.Foundation.Basic.Utility;

namespace ConfigPLSQLClient.UI
{
    public partial class MainForm : Form
    {
        private static String oracleClientDirNameForPLSQL = "oracleClientFilesForPLSQL";

        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 执行安装按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(textBox1.Text))
                {
                    MessageBoxEx.Show(this, "请先选中安装目录！", "提醒");
                    return;
                }
                Thread t = new Thread(new ParameterizedThreadStart((o) =>
                {
                    DoSetup(o.ToString());
                }));
                MyWaitingForm.Show(this, "正在安装配置，请等候", "提醒", false, (showWin, showE) =>
                {
                    //需要访问窗口的值，所以需要用Invoke
                    MyControl.ControlInvoke(this, new Action<Object>((oParam) =>
                    {
                        try
                        {
                            t.Start(textBox1.Text);
                        }
                        catch (Exception ex)
                        {
                            MyLog.MakeLog(ex);
                            MyWaitingForm.WaitingForm.Invoke(new Action<Object>((oEmpty) =>
                            {
                                MessageBoxEx.Show(MyWaitingForm.WaitingForm, "安装过程发生异常！", "提醒");
                            }), "");
                            MyWaitingForm.Hide();
                        }
                    }), "");
                });
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
                MyWaitingForm.Hide();
                MessageBoxEx.Show(this, "安装过程发生异常！", "提醒");
            }
        }

        private static void DoSetup(String setupPath)
        {
            try
            {
                if (CopyFile(setupPath))
                {
                    ConfigEnvironmentVariable(Path.Combine(setupPath, oracleClientDirNameForPLSQL));
                    MyWaitingForm.WaitingForm.Invoke(new Action<Object>((oEmpty) =>
                    {
                        MessageBoxEx.Show(MyWaitingForm.WaitingForm, "安装成功！", "提醒");
                    }), "");
                }
            }
            catch (UnauthorizedAccessException)
            {
                MyWaitingForm.WaitingForm.Invoke(new Action<Object>((oEmpty) =>
                {
                    MessageBoxEx.Show(MyWaitingForm.WaitingForm, "请以管理员的身份运行程序！", "提醒");
                }), "");
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
                MyWaitingForm.WaitingForm.Invoke(new Action<Object>((oEmpty) =>
                {
                    MessageBoxEx.Show(MyWaitingForm.WaitingForm, "安装过程发生异常！", "提醒");
                }), "");
            }
            finally
            {
                MyWaitingForm.Hide();
            }
        }

        /// <summary>
        /// 复制所需要的文件
        /// </summary>
        /// <param name="setupPATH"></param>
        public static Boolean CopyFile(String setupPath)
        {
            String filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, oracleClientDirNameForPLSQL);
            if (Directory.Exists(filePath))
            {
                MyIO.DirCopy(filePath, setupPath, true);
                return true;
            }
            else
            {
                MyWaitingForm.WaitingForm.Invoke(new Action<Object>((oEmpty) =>
                {
                    MessageBoxEx.Show(MyWaitingForm.WaitingForm, String.Format("无法找到目录：{0}", oracleClientDirNameForPLSQL), "提醒");
                }), "");
            }
            return false;
        }

        /// <summary>
        /// 配置oracle客户端的环境变量
        /// </summary>
        /// <param name="setupPath">安装目录</param>
        public static void ConfigEnvironmentVariable(String setupPath)
        {
            Environment.SetEnvironmentVariable("ORACLE_HOME", setupPath, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable("TNS_ADMIN", setupPath, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable("NLS_LANG", "SIMPLIFIED CHINESE_CHINA.ZHS16GBK", EnvironmentVariableTarget.Machine);

            //获取path的值
            String path = Environment.GetEnvironmentVariable("path", EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable("path", path + ";" + setupPath, EnvironmentVariableTarget.Machine);
        }

        /// <summary>
        /// 选择目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
